﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace ParticleFiltersDemo1
{
    public class ParticleSetTemplatedControl : Control
    {
        private int blockSize = 50;
        private int canvasXOffset = 10;
        private int canvasYOffset = 10;

        private Canvas plotArea;
        private bool isControllInitialized = false;

        public static readonly DependencyProperty ParticlesProperty =
            DependencyProperty.Register( "Particles", typeof( ParticlesSet ), typeof( ParticleSetTemplatedControl ),
            new PropertyMetadata( null, new PropertyChangedCallback( OnParticlesPropertyChanged ) ) );

        public ParticlesSet Particles
        {
            get { return ( ParticlesSet )GetValue( ParticlesProperty ); }
            set { SetValue( ParticlesProperty, value ); }
        }

        public ParticleSetTemplatedControl()
        {
            this.DefaultStyleKey = typeof( ParticleSetTemplatedControl );
        }

        public void PositionChanged()
        {
            // this.SetValue( Canvas.TopProperty, ( object )( ( this.Partickle.Y ) * stepSize + 15 ) );
            // this.SetValue( Canvas.LeftProperty, ( object )( ( this.Partickle.X ) * stepSize + 15 ) );
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            Canvas parentCanvas = this.Parent as Canvas;
            Debug.Assert( parentCanvas != null );

            this.plotArea = this.GetTemplateChild( "plotArea" ) as Canvas;

            /* Test
            this.DrawSingleParticle( 0, 0, 0 );
            this.DrawSingleParticle( 3, 4, 90 );
            this.DrawSingleParticle( 9, 9, -90 );
            this.DrawSingleParticle( 5, 4, 180 );
            */

            if ( this.Particles != null )
                this.DrawParticles();
        }

        private void DrawParticles()
        {
            this.plotArea.Children.Clear();
            foreach ( Particle particle in this.Particles.particles )
            {
                this.DrawSingleParticle( ( int )particle.X, ( int )particle.Y, ( int )particle.heading );
            }
        }

        private void DrawSingleParticle( int x, int y, int orientation )
        {
            Polygon triangle = new Polygon()
            {
                Fill = new SolidColorBrush( Colors.White ),
                Stroke = new SolidColorBrush( Colors.Green )
            };

            int xOffset = x * this.blockSize + canvasXOffset;
            int yOffset = y * this.blockSize + canvasYOffset;

            int topX = 25;
            int leftX = 18;
            int rightX = 30;

            int topY = 15;
            int leftY = 30;
            int rightY = 30;

            if ( orientation == 180 )
            {
                topY = 30;
                leftY = 15;
                rightY = 15;
            }
            else if ( orientation == 90 )
            {
                topX = 18; topY = 25;
                leftX = 30; leftY = 18;
                rightX = 30; rightY = 30;
            }
            else if ( orientation == -90 )
            {
                topX = 30; topY = 25;
                leftX = 18; leftY = 18;
                rightX = 18; rightY = 30;
            }

            triangle.Points.Add( new Point( topX + xOffset, topY + yOffset ) );
            triangle.Points.Add( new Point( leftX + xOffset, leftY + yOffset ) );
            triangle.Points.Add( new Point( rightX + xOffset, rightY + yOffset ) );

            this.plotArea.Children.Add( triangle );
        }

        private static void OnParticlesPropertyChanged( DependencyObject target, DependencyPropertyChangedEventArgs eh )
        {
            ParticleSetTemplatedControl control = target as ParticleSetTemplatedControl;
            if ( control.isControllInitialized == true && control != null && control.Particles != null )
            {
                control.DrawParticles();
            }
        }
    }
}
